using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class JobSeekers_education : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else        
        {
            string educationID = 
            Request.QueryString.Get("EducationID");
            
            if(!IsPostBack  )
            {
                Common cls = new Common(connectionString);
                DataTable dataTable = 
                cls.GetDataTable("Select * from "+
                " tblJSCertificates" );
                DropDownListHelper helper = 
                new DropDownListHelper();
                helper.FillDataTableToDropDownList(
                dataTable, ddlCertificate,
                "CertificateName" , "CertificateID" , "" );
                EditEducation(educationID);
            }
        }
    }
    
    void EditEducation(string educationID)
    {
        if (educationID != "" && educationID != null )
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = 
            "select * from tblJSEducations" +
            " where UserID=@UserID and " +
            " EducationID= @EducationID ";
            cls.ParameterCollection = new 
            string[2] {"@UserID","@EducationID"};
            cls.ValueCollection = new string[2] 
            {Session["jsuserid"].ToString() , 
            educationID};
            DataRow dataRow = 
            cls.GetDataRow( );
            if (dataRow != null)
            {
                EducationID.Value = dataRow[0].ToString();
                txtSchool.Text = dataRow[2].ToString();
                ddlCountry.SelectedValue = dataRow[3].ToString();
                txtFaculty.Text = dataRow[4].ToString();
                txtMajor.Text = dataRow[6].ToString();
                ddlCertificate.SelectedValue = dataRow[5].ToString();
                rblMode.SelectedValue = dataRow[7].ToString();
                txtFrom.Text = dataRow[8].ToString();
                txtTo.Text = dataRow[9].ToString();
                txtDescription.Text = dataRow[10].ToString();
                btnSave.Text = "Update";
                btnMore.Visible = false;
            }
            
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(Session.Contents["jsuserid"]) != "")
            Response.Redirect("experiences.aspx");
        else
            Response.Redirect("experience.aspx");
    }
    protected void btnMore_Click(object sender, EventArgs e)
    {         
        Save("0");
        Response.Redirect("education.aspx");
    }
    void Save(string id)
    {
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = connectionString;
        if (!cls.AddNewOrUpdateEducation(id,
        Convert.ToString(Session.Contents["jsuserid"]),
        txtSchool.Text,
        ddlCountry.SelectedValue,
        txtFaculty.Text,
        ddlCertificate.SelectedValue,
        txtMajor.Text,        
        rblMode.SelectedValue,
        txtFrom.Text, txtTo.Text,
        txtDescription.Text))
            literalError.Text =
            "Error: " + cls.ErrorMessage;
        
            
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = "0";
        if(EducationID.Value != "") 
            id = EducationID.Value;
        Save(id);
        if (id != "0")
            Response.Redirect("educations.aspx");
        else
            Response.Redirect("education.aspx");       
    }
}
